Merge "rdbms: make LBFactory close/rollback dangling handles like LoadBalancer"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / BagOStuffTest.php
1 <?php
2
3 use Wikimedia\ScopedCallback;
4 use Wikimedia\TestingAccessWrapper;
5
6 /**
7 * @author Matthias Mullie <mmullie@wikimedia.org>
8 * @group BagOStuff
9 * @covers BagOStuff
10 */
11 class BagOStuffTest extends MediaWikiTestCase {
12 /** @var BagOStuff */
13 private $cache;
14
15 const TEST_KEY = 'test';
16
17 protected function setUp() {
18 parent::setUp();
19
20 // type defined through parameter
21 if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
22 global $wgObjectCaches;
23
24 $id = $this->getCliArg( 'use-bagostuff' );
25 $this->cache = ObjectCache::newFromParams( $wgObjectCaches[$id] );
26 } else {
27 // no type defined - use simple hash
28 $this->cache = new HashBagOStuff;
29 }
30
31 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) );
32 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) . ':lock' );
33 }
34
35 /**
36 * @covers MediumSpecificBagOStuff::makeGlobalKey
37 * @covers MediumSpecificBagOStuff::makeKeyInternal
38 */
39 public function testMakeKey() {
40 $cache = new HashBagOStuff();
41
42 $localKey = $cache->makeKey( 'first', 'second', 'third' );
43 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
44
45 $this->assertStringMatchesFormat(
46 '%Sfirst%Ssecond%Sthird%S',
47 $localKey,
48 'Local key interpolates parameters'
49 );
50
51 $this->assertStringMatchesFormat(
52 'global%Sfirst%Ssecond%Sthird%S',
53 $globalKey,
54 'Global key interpolates parameters and contains global prefix'
55 );
56
57 $this->assertNotEquals(
58 $localKey,
59 $globalKey,
60 'Local key and global key with same parameters should not be equal'
61 );
62
63 $this->assertNotEquals(
64 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
65 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
66 );
67 }
68
69 /**
70 * @covers MediumSpecificBagOStuff::merge
71 * @covers MediumSpecificBagOStuff::mergeViaCas
72 */
73 public function testMerge() {
74 $key = $this->cache->makeKey( self::TEST_KEY );
75
76 $calls = 0;
77 $casRace = false; // emulate a race
78 $callback = function ( BagOStuff $cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
79 ++$calls;
80 if ( $casRace ) {
81 // Uses CAS instead?
82 $cache->set( $key, 'conflict', 5 );
83 }
84
85 return ( $oldVal === false ) ? 'merged' : $oldVal . 'merged';
86 };
87
88 // merge on non-existing value
89 $merged = $this->cache->merge( $key, $callback, 5 );
90 $this->assertTrue( $merged );
91 $this->assertEquals( 'merged', $this->cache->get( $key ) );
92
93 // merge on existing value
94 $merged = $this->cache->merge( $key, $callback, 5 );
95 $this->assertTrue( $merged );
96 $this->assertEquals( 'mergedmerged', $this->cache->get( $key ) );
97
98 $calls = 0;
99 $casRace = true;
100 $this->assertFalse(
101 $this->cache->merge( $key, $callback, 5, 1 ),
102 'Non-blocking merge (CAS)'
103 );
104
105 if ( $this->cache instanceof MultiWriteBagOStuff ) {
106 $wrapper = TestingAccessWrapper::newFromObject( $this->cache );
107 $this->assertEquals( count( $wrapper->caches ), $calls );
108 } else {
109 $this->assertEquals( 1, $calls );
110 }
111 }
112
113 /**
114 * @covers MediumSpecificBagOStuff::changeTTL
115 */
116 public function testChangeTTLRenew() {
117 $now = microtime( true ); // need real time
118 $this->cache->setMockTime( $now );
119
120 $key = $this->cache->makeKey( self::TEST_KEY );
121 $value = 'meow';
122
123 $this->cache->add( $key, $value, 60 );
124 $this->assertEquals( $value, $this->cache->get( $key ) );
125 $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
126 $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
127 $this->assertTrue( $this->cache->changeTTL( $key, 0 ) );
128 $this->assertEquals( $this->cache->get( $key ), $value );
129
130 $this->cache->delete( $key );
131 $this->assertFalse( $this->cache->changeTTL( $key, 15 ) );
132 }
133
134 /**
135 * @covers MediumSpecificBagOStuff::changeTTL
136 */
137 public function testChangeTTLExpireRel() {
138 $now = microtime( true ); // need real time
139 $this->cache->setMockTime( $now );
140
141 $key = $this->cache->makeKey( self::TEST_KEY );
142 $value = 'meow';
143
144 $this->cache->add( $key, $value, 5 );
145 $this->assertTrue( $this->cache->changeTTL( $key, -3600 ) );
146 $this->assertFalse( $this->cache->get( $key ) );
147 }
148
149 /**
150 * @covers MediumSpecificBagOStuff::changeTTL
151 */
152 public function testChangeTTLExpireAbs() {
153 $now = microtime( true ); // need real time
154 $this->cache->setMockTime( $now );
155
156 $key = $this->cache->makeKey( self::TEST_KEY );
157 $value = 'meow';
158
159 $this->cache->add( $key, $value, 5 );
160 $this->assertTrue( $this->cache->changeTTL( $key, $now - 3600 ) );
161 $this->assertFalse( $this->cache->get( $key ) );
162 }
163
164 /**
165 * @covers MediumSpecificBagOStuff::changeTTLMulti
166 */
167 public function testChangeTTLMulti() {
168 $now = 1563892142;
169 $this->cache->setMockTime( $now );
170
171 $key1 = $this->cache->makeKey( 'test-key1' );
172 $key2 = $this->cache->makeKey( 'test-key2' );
173 $key3 = $this->cache->makeKey( 'test-key3' );
174 $key4 = $this->cache->makeKey( 'test-key4' );
175
176 // cleanup
177 $this->cache->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
178
179 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
180 $this->assertFalse( $ok, "No keys found" );
181 $this->assertFalse( $this->cache->get( $key1 ) );
182 $this->assertFalse( $this->cache->get( $key2 ) );
183 $this->assertFalse( $this->cache->get( $key3 ) );
184
185 $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
186 $this->assertTrue( $ok, "setMulti() succeeded" );
187 $this->assertEquals(
188 3,
189 count( $this->cache->getMulti( [ $key1, $key2, $key3 ] ) ),
190 "setMulti() succeeded via getMulti() check"
191 );
192
193 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
194 $this->assertTrue( $ok, "TTL bumped for all keys" );
195 $this->assertEquals( 1, $this->cache->get( $key1 ) );
196 $this->assertEquals( 2, $this->cache->get( $key2 ) );
197 $this->assertEquals( 3, $this->cache->get( $key3 ) );
198
199 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
200 $this->assertFalse( $ok, "One key missing" );
201 $this->assertEquals( 1, $this->cache->get( $key1 ), "Key still live" );
202
203 $now = microtime( true ); // real time
204 $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
205 $this->assertTrue( $ok, "setMulti() succeeded" );
206
207 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], $now + 86400 );
208 $this->assertTrue( $ok, "Expiry set for all keys" );
209 $this->assertEquals( 1, $this->cache->get( $key1 ), "Key still live" );
210
211 $this->assertEquals( 2, $this->cache->incr( $key1 ) );
212 $this->assertEquals( 3, $this->cache->incr( $key2 ) );
213 $this->assertEquals( 4, $this->cache->incr( $key3 ) );
214
215 // cleanup
216 $this->cache->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
217 }
218
219 /**
220 * @covers MediumSpecificBagOStuff::add
221 */
222 public function testAdd() {
223 $key = $this->cache->makeKey( self::TEST_KEY );
224 $this->assertFalse( $this->cache->get( $key ) );
225 $this->assertTrue( $this->cache->add( $key, 'test', 5 ) );
226 $this->assertFalse( $this->cache->add( $key, 'test', 5 ) );
227 }
228
229 /**
230 * @covers MediumSpecificBagOStuff::get
231 */
232 public function testGet() {
233 $value = [ 'this' => 'is', 'a' => 'test' ];
234
235 $key = $this->cache->makeKey( self::TEST_KEY );
236 $this->cache->add( $key, $value, 5 );
237 $this->assertEquals( $this->cache->get( $key ), $value );
238 }
239
240 /**
241 * @covers MediumSpecificBagOStuff::get
242 * @covers MediumSpecificBagOStuff::set
243 * @covers MediumSpecificBagOStuff::getWithSetCallback
244 */
245 public function testGetWithSetCallback() {
246 $now = 1563892142;
247 $cache = new HashBagOStuff( [] );
248 $cache->setMockTime( $now );
249 $key = $cache->makeKey( self::TEST_KEY );
250
251 $this->assertFalse( $cache->get( $key ), "No value" );
252
253 $value = $cache->getWithSetCallback(
254 $key,
255 30,
256 function ( &$ttl ) {
257 $ttl = 10;
258
259 return 'hello kitty';
260 }
261 );
262
263 $this->assertEquals( 'hello kitty', $value );
264 $this->assertEquals( $value, $cache->get( $key ), "Value set" );
265
266 $now += 11;
267
268 $this->assertFalse( $cache->get( $key ), "Value expired" );
269 }
270
271 /**
272 * @covers MediumSpecificBagOStuff::incr
273 */
274 public function testIncr() {
275 $key = $this->cache->makeKey( self::TEST_KEY );
276 $this->cache->add( $key, 0, 5 );
277 $this->cache->incr( $key );
278 $expectedValue = 1;
279 $actualValue = $this->cache->get( $key );
280 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
281 }
282
283 /**
284 * @covers MediumSpecificBagOStuff::incrWithInit
285 */
286 public function testIncrWithInit() {
287 $key = $this->cache->makeKey( self::TEST_KEY );
288 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
289 $this->assertEquals( 3, $val, "Correct init value" );
290
291 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
292 $this->assertEquals( 4, $val, "Correct init value" );
293 $this->cache->delete( $key );
294
295 $val = $this->cache->incrWithInit( $key, 0, 5 );
296 $this->assertEquals( 5, $val, "Correct init value" );
297 }
298
299 /**
300 * @covers MediumSpecificBagOStuff::getMulti
301 */
302 public function testGetMulti() {
303 $value1 = [ 'this' => 'is', 'a' => 'test' ];
304 $value2 = [ 'this' => 'is', 'another' => 'test' ];
305 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
306 $value4 = [ 'another test where chars in key will be encoded' ];
307
308 $key1 = $this->cache->makeKey( 'test-1' );
309 $key2 = $this->cache->makeKey( 'test-2' );
310 // internally, MemcachedBagOStuffs will encode to will-%25-encode
311 $key3 = $this->cache->makeKey( 'will-%-encode' );
312 $key4 = $this->cache->makeKey(
313 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
314 );
315
316 // cleanup
317 $this->cache->delete( $key1 );
318 $this->cache->delete( $key2 );
319 $this->cache->delete( $key3 );
320 $this->cache->delete( $key4 );
321
322 $this->cache->add( $key1, $value1, 5 );
323 $this->cache->add( $key2, $value2, 5 );
324 $this->cache->add( $key3, $value3, 5 );
325 $this->cache->add( $key4, $value4, 5 );
326
327 $this->assertEquals(
328 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
329 $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
330 );
331
332 // cleanup
333 $this->cache->delete( $key1 );
334 $this->cache->delete( $key2 );
335 $this->cache->delete( $key3 );
336 $this->cache->delete( $key4 );
337 }
338
339 /**
340 * @covers MediumSpecificBagOStuff::setMulti
341 * @covers MediumSpecificBagOStuff::deleteMulti
342 */
343 public function testSetDeleteMulti() {
344 $map = [
345 $this->cache->makeKey( 'test-1' ) => 'Siberian',
346 $this->cache->makeKey( 'test-2' ) => [ 'Huskies' ],
347 $this->cache->makeKey( 'test-3' ) => [ 'are' => 'the' ],
348 $this->cache->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
349 $this->cache->makeKey( 'test-5' ) => 4,
350 $this->cache->makeKey( 'test-6' ) => 'ever'
351 ];
352
353 $this->assertTrue( $this->cache->setMulti( $map ) );
354 $this->assertEquals(
355 $map,
356 $this->cache->getMulti( array_keys( $map ) )
357 );
358
359 $this->assertTrue( $this->cache->deleteMulti( array_keys( $map ) ) );
360
361 $this->assertEquals(
362 [],
363 $this->cache->getMulti( array_keys( $map ), BagOStuff::READ_LATEST )
364 );
365 $this->assertEquals(
366 [],
367 $this->cache->getMulti( array_keys( $map ) )
368 );
369 }
370
371 /**
372 * @covers MediumSpecificBagOStuff::get
373 * @covers MediumSpecificBagOStuff::getMulti
374 * @covers MediumSpecificBagOStuff::merge
375 * @covers MediumSpecificBagOStuff::delete
376 */
377 public function testSetSegmentable() {
378 $key = $this->cache->makeKey( self::TEST_KEY );
379 $tiny = 418;
380 $small = wfRandomString( 32 );
381 // 64 * 8 * 32768 = 16777216 bytes
382 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
383
384 $callback = function ( $cache, $key, $oldValue ) {
385 return $oldValue . '!';
386 };
387
388 $cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ];
389 foreach ( $cases as $case => $value ) {
390 $this->cache->set( $key, $value, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
391 $this->assertEquals( $value, $this->cache->get( $key ), "get $case" );
392 $this->assertEquals( $value, $this->cache->getMulti( [ $key ] )[$key], "get $case" );
393
394 $this->assertTrue(
395 $this->cache->merge( $key, $callback, 5, 1, BagOStuff::WRITE_ALLOW_SEGMENTS ),
396 "merge $case"
397 );
398 $this->assertEquals(
399 "$value!",
400 $this->cache->get( $key ),
401 "merged $case"
402 );
403 $this->assertEquals(
404 "$value!",
405 $this->cache->getMulti( [ $key ] )[$key],
406 "merged $case"
407 );
408
409 $this->assertTrue( $this->cache->deleteMulti( [ $key ] ), "delete $case" );
410 $this->assertFalse( $this->cache->get( $key ), "deleted $case" );
411 $this->assertEquals( [], $this->cache->getMulti( [ $key ] ), "deletd $case" );
412
413 $this->cache->set( $key, "@$value", 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
414 $this->assertEquals( "@$value", $this->cache->get( $key ), "get $case" );
415 $this->assertTrue(
416 $this->cache->delete( $key, BagOStuff::WRITE_PRUNE_SEGMENTS ),
417 "prune $case"
418 );
419 $this->assertFalse( $this->cache->get( $key ), "pruned $case" );
420 $this->assertEquals( [], $this->cache->getMulti( [ $key ] ), "pruned $case" );
421 }
422
423 $this->cache->set( $key, 666, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
424
425 $this->assertEquals( 666, $this->cache->get( $key ) );
426 $this->assertEquals( 667, $this->cache->incr( $key ) );
427 $this->assertEquals( 667, $this->cache->get( $key ) );
428
429 $this->assertEquals( 664, $this->cache->decr( $key, 3 ) );
430 $this->assertEquals( 664, $this->cache->get( $key ) );
431
432 $this->assertTrue( $this->cache->delete( $key ) );
433 $this->assertFalse( $this->cache->get( $key ) );
434 }
435
436 /**
437 * @covers MediumSpecificBagOStuff::getScopedLock
438 */
439 public function testGetScopedLock() {
440 $key = $this->cache->makeKey( self::TEST_KEY );
441 $value1 = $this->cache->getScopedLock( $key, 0 );
442 $value2 = $this->cache->getScopedLock( $key, 0 );
443
444 $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
445 $this->assertNull( $value2, 'Duplicate call returned no lock' );
446
447 unset( $value1 );
448
449 $value3 = $this->cache->getScopedLock( $key, 0 );
450 $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
451 unset( $value3 );
452
453 $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
454 $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
455
456 $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
457 $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
458 }
459
460 /**
461 * @covers MediumSpecificBagOStuff::__construct
462 * @covers MediumSpecificBagOStuff::trackDuplicateKeys
463 */
464 public function testReportDupes() {
465 $logger = $this->createMock( Psr\Log\NullLogger::class );
466 $logger->expects( $this->once() )
467 ->method( 'warning' )
468 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
469 'key' => 'foo',
470 'count' => 2,
471 ] );
472
473 $cache = new HashBagOStuff( [
474 'reportDupes' => true,
475 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
476 'logger' => $logger,
477 ] );
478 $cache->get( 'foo' );
479 $cache->get( 'bar' );
480 $cache->get( 'foo' );
481
482 DeferredUpdates::doUpdates();
483 }
484
485 /**
486 * @covers MediumSpecificBagOStuff::lock()
487 * @covers MediumSpecificBagOStuff::unlock()
488 */
489 public function testLocking() {
490 $key = 'test';
491 $this->assertTrue( $this->cache->lock( $key ) );
492 $this->assertFalse( $this->cache->lock( $key ) );
493 $this->assertTrue( $this->cache->unlock( $key ) );
494
495 $key2 = 'test2';
496 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
497 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
498 $this->assertTrue( $this->cache->unlock( $key2 ) );
499 $this->assertTrue( $this->cache->unlock( $key2 ) );
500 }
501
502 public function tearDown() {
503 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) );
504 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) . ':lock' );
505
506 parent::tearDown();
507 }
508 }